package com.sanuo.pico.act.work;

import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.menu.app.util.Utils;
import com.plata.base.aclr.common.SilentTask;
import com.plata.base.aclr.common.TaskEngine;
import com.sanuo.pico.R;
import com.sanuo.pico.databinding.ActivityMsnBinding;
import com.sanuo.pico.event.CountEvent;
import com.sanuo.pico.event.Event;
import com.sanuo.pico.it.SettingPage;
import com.sanuo.pico.utils.Command;
import com.sanuo.pico.utils.Const;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class CountAct extends BaseWorkActivity implements SettingPage {

    ActivityMsnBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMsnBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.back.setOnClickListener(v -> {
            Command.sendTouchEvent(0, 0x20);
            if (Const.jump) {
                finish();
            }
        });

        binding.title.setText(getResources().getStringArray(R.array.settings)[6]);

        binding.tv2.setInputType(InputType.TYPE_CLASS_NUMBER);
        binding.tv2.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView arg0, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH
                        || (event != null && actionId == EditorInfo.IME_ACTION_UNSPECIFIED && event.getAction() == KeyEvent.ACTION_DOWN)
                        || actionId == EditorInfo.IME_ACTION_DONE
                        || (event != null
                        && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event
                        .getAction() == KeyEvent.ACTION_DOWN)) {
                    String requestWord = binding.tv2.getText().toString().trim();
                    if (TextUtils.isEmpty(requestWord)) {

                    } else {
                        sendPwd(requestWord);
                    }
                    return true;
                }
                return false;
            }
        });

        binding.save.setOnClickListener(v -> Command.sendTouchEvent(0, 0x43));

        onMessageEvent(Event.count);
    }

    private void sendPwd(String s) {
        Utils.hideInput(binding.tv2);
        h.removeCallbacksAndMessages(null);
        showLoading();
        TaskEngine.getInstance().submit(new SilentTask("pwd") {
            @Override
            public void runInBackground() throws Exception {
                long sleep = 200;
                Command.sendTouchEvent(0, 0x5a);
                Thread.sleep(sleep);
                char[] array = s.toCharArray();
                for (char c : array) {
                    int i = c - '0';
                    Command.sendTouchEvent(0, 0x50 + i);
                    Thread.sleep(sleep);
                }
                Command.sendTouchEvent(0, 0x5c);
                h.post(() -> hideLoading());
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(CountEvent event) {
        binding.tv.setText(event.getValue() + "");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (Const.is1315()) {
            Command.getRented();
        }
    }
}
